<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class ConfigComponent
{
	function startup(Controller $Controller)
	{
		if (Core::config_autoload)
		{
			# website
			$this->load(APP.'config'.DS.'config.json');
			
			# plugin
			if (!empty(Load::$plugin))
				$this->load(APP.'plugins'.DS.Load::$plugin.DS.'config'.DS.'config.json');
		}
	}
	
	function load($data)
	{
		if (!is_array($data))
			$data = json_decode(file_get_contents($data), true);
		foreach($data as $k => $v)
			$this->$k = $v;
	}
	
	function append($data)
	{
		if (!is_array($data))
			$data = json_decode(file_get_contents($data), true);
		foreach($data as $k => $v)
		{
			foreach($v as $kk => $vv)
				$this->{$k}[$kk] = $vv;
		}
	}
	
	function save($file, $what=null)
	{
		# what?
		if (!empty($what) and !is_array($what))
			$what = explode(',', $what);
		if (empty($what))
		{
			$what = array();
			foreach($this as $k => $v)
				$what[] = $k;
		}
		
		# get data
		$data = array();
		foreach($what as $k)
			$data[$k] = $this->$k;
		
		# save
		file_put_contents($file, json_encode($data));
#		chmod($file, 0775);
	}
}